import { Loading } from 'element-ui'
 
let loadingCount = 0
let loading
 
const startLoading = (options = {}) => {
  loading = Loading.service({
    lock: true,
    text: '加载中……',
    background: 'rgba(255, 255, 255, 0.8)',
    ...options
  })
}
 
const endLoading = () => {
  setTimeout(() => {
    loading.close()
  }, 300)
}
 
export const showLoading = (options) => {
  if (loadingCount === 0) {
      startLoading(options)
  }
  loadingCount += 1
}
 
export const hideLoading = () => {
  if (loadingCount <= 0) {
    return
  }
  loadingCount -= 1
  if (loadingCount === 0) {
    endLoading()
  }
}

export default function(Vue) {
  //添加全局API
  Vue.prototype.$loading = {
    showLoading,
    hideLoading
  }
}